//
// This file is part of the GNU ARM Eclipse distribution.
// Copyright (c) 2014 Liviu Ionescu.
//

// ----------------------------------------------------------------------------

#include <stdio.h>
#include "diag/Trace.h"
#include "stm32f30x_conf.h"

#include "Scheduler.h"

#include "Task.h"
#include "LedBlink.h"
#include "TestTask.h"
#include "RCreceiver.h"

// ----------------------------------------------------------------------------
//
// STM32F3 empty sample (trace via ITM).
//
// Trace support is enabled by adding the TRACE macro definition.
// By default the trace messages are forwarded to the ITM output,
// but can be rerouted to any device or completely suppressed, by
// changing the definitions required in system/src/diag/trace_impl.c
// (currently OS_USE_TRACE_ITM, OS_USE_TRACE_SEMIHOSTING_DEBUG/_STDOUT).
//

// ----- main() ---------------------------------------------------------------

// Sample pragmas to cope with warnings. Please note the related line at
// the end of this function, used to pop the compiler diagnostics status.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#pragma GCC diagnostic ignored "-Wreturn-type"

/* Global variables */
uint32_t globalFlags = 0;
Status status;
Scheduler scheduler(&status);

/* create rc receiver
 * TODO: select default receiver prio
 * */
RCreceiver rcRec(&status, 5);

int main(int argc, char* argv[]) {
	// At this stage the system clock should have already been configured
	// at high speed.

	/* create Tasks and Array */

	LedBlink led3(&status, 1);
	led3.setLED(LED3);
	led3.setFrequency(2);
	LedBlink led4(&status, 1);
	led4.setLED(LED4);
	led4.setFrequency(4);
	LedBlink led5(&status, 1);
	led5.setLED(LED5);
	led5.setFrequency(8);
	LedBlink led6(&status, 1);
	led6.setLED(LED6);
	led6.setFrequency(16);
	LedBlink led7(&status, 1);
	led7.setLED(LED7);
	led7.setFrequency(32);
	LedBlink led8(&status, 1);
	led8.setLED(LED8);
	led8.setFrequency(1);
	LedBlink led9(&status, 1);
	led9.setLED(LED9);
	led9.setFrequency(0.5);
	LedBlink led10(&status, 1);
	led10.setLED(LED10);
	led10.setFrequency(0.1);

	TestTask* t1 = new TestTask(&status, 0);
	t1->setCounter(0x800);
	t1->setLED(LED3);
	TestTask* t2 = new TestTask(&status, 0);
	t2->setCounter(0x800);
	t2->setLED(LED5);
	TestTask* t3 = new TestTask(&status, 0);
	t3->setCounter(0x800);
	t3->setLED(LED7);
	TestTask* t4 = new TestTask(&status, 3);
	t4->setCounter(0x800);
	t4->setLED(LED9);
	TestTask* t5 = new TestTask(&status, 4);
	t5->setCounter(0x800);
	t5->setLED(LED10);
	TestTask* t6 = new TestTask(&status, 5);
	t6->setCounter(0x800);
	t6->setLED(LED8);
	TestTask* t7 = new TestTask(&status, 6);
	t7->setCounter(0x800);
	t7->setLED(LED6);
	TestTask* t8 = new TestTask(&status, 7);
	t8->setCounter(0x50000);
	t8->setLED(LED4);

	Task* taskarray[] = { &led3, &led4, &led5, &led6, &led7, &led8, &led9,
			&led10 };

	scheduler.start(taskarray, 8);
	// Infinite loop
	while (1) {
		// Add your code here.
	}
}

extern "C" {

void TIM2_IRQHandler() {

	scheduler.timerIRQ();
	TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}

void TIM4_IRQHandler(void) {

		rcRec.RCreceiverIRQ();

	if (TIM_GetITStatus(TIM4, TIM_IT_CC3) == SET) {
		TIM_ClearITPendingBit(TIM4, TIM_IT_CC3);
	}
	if (TIM_GetITStatus(TIM4, TIM_IT_Update) == SET) {
		TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
	}
}

}

#pragma GCC diagnostic pop

// ----------------------------------------------------------------------------
